﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using PlanningGIS.Util;
using PlanningGIS.Util.Management;

namespace PlanningGIS.ManageUtility.Modules
{
    /// <summary>
    /// 日志操作类
    /// </summary>
    public class Log
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sn">记录的顺序号，批量操作时设置一下顺序号，减少ID重复的概率</param>
        public Log(int sn=0)
        {
            ID = DateTime.Now.ToString("yyyyMMddHHmmssfff") + sn.ToString().PadLeft(4, '0');
            IP = OSHelper.GetIP();
            OP_TIME = DateTime.Now;
            ISDELETED = 0;
        }

        /// <summary>
        /// 唯一编号,年月日时分秒毫秒+顺序号
        /// </summary>
        public string ID { get; set; }

        /// <summary>
        /// 用户唯一编号
        /// </summary>
        public string USER_ID { get; set; }

        /// <summary>
        /// 用户名称
        /// </summary>
        public string USER_NAME { get; set; }

        /// <summary>
        /// 登录的IP地址
        /// </summary>
        public string IP { get; set; }

        /// <summary>
        /// 操作类型
        /// </summary>
        public zjgisLogType OP_TYPE { get; set; }

        /// <summary>
        /// 相关的数据
        /// </summary>
        public string DATA_INVOLOVE { get; set; }

        /// <summary>
        /// 日志信息
        /// </summary>
        public string NOTES { get; set; }

        /// <summary>
        /// 操作时间
        /// </summary>
        public DateTime OP_TIME { get; set; }

        /// <summary>
        /// 操作状态
        /// </summary>
        public zjgisLogStatus OP_STATUS { get; set; }

        /// <summary>
        /// 是否被删除，0没有删除，1被删除
        /// </summary>
        public int ISDELETED { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        public string REMARK { get; set; }

        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="log"></param>
        public static void WriteLog(Log log)
        {
            if (log == null) return;
            if (ManageHelper.DBContext == null) return;
            if (string.IsNullOrWhiteSpace(log.ID))
                log.ID = DateTime.Now.ToString("yyyyMMddHHmmssttt") + "0000";
            Dictionary<string, dynamic> values = new Dictionary<string, dynamic>();
            try
            {
                values.Add("ID", log.ID);
                values.Add("USER_ID", log.USER_ID);
                values.Add("USER_NAME", log.USER_NAME);
                values.Add("IP", log.IP);
                values.Add("OP_TYPE", log.OP_TYPE.GetDescription());
                values.Add("DATA_INVOLOVE", log.DATA_INVOLOVE);
                values.Add("OP_STATUS", log.OP_STATUS.GetDescription());
                values.Add("NOTES", log.NOTES);
                values.Add("OP_TIME", log.OP_TIME); //REMARK.ToString("yyyy-MM-dd HH:mm:ss")
                values.Add("REMARK", log.REMARK);
                values.Add("ISDELETED", 0);
                ManageHelper.DBContext.Insert("LOG", values);
            }
            catch (Exception ex)
            {
                PlanningGIS.Util.Log.LogHelper.WriteErrorText(ex);
            }
            
        }

    }

    /// <summary>
    /// 日志类型
    /// </summary>
    public enum zjgisLogType
    {
        /// <summary>
        /// 登录
        /// </summary>
        [Description("登录")]
        Login,

        /// <summary>
        /// 数据操作
        /// </summary>
        [Description("数据操作")]
        AccessData,

        /// <summary>
        /// 修改密码有效期
        /// </summary>
        [Description("修改信息")]
        Modify,

        /// <summary>
        /// 新增用户
        /// </summary>
        [Description("用户管理")]
        User,

        /// <summary>
        /// 删除日志
        /// </summary>
        [Description("删除日志")]
        DeleteLog

    }

    /// <summary>
    /// 日志状态
    /// </summary>
    public enum zjgisLogStatus
    {
        /// <summary>
        /// 成功
        /// </summary>
        [Description("成功")]
        Success,

        /// <summary>
        /// 失败
        /// </summary>
        [Description("失败")]
        Fail
    }

}
